package ar.com.mooral.activities;

import java.util.Collections;
import java.util.Hashtable;
import java.util.Vector;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.crohn.view.CrohnButton;
import ar.com.mooral.crohn.view.CrohnImageView;
import ar.com.mooral.data.GeneralStatus;
import ar.com.mooral.data.Medications;
import ar.com.mooral.data.MedicationsList;
import ar.com.mooral.data.MissedMedications;
import ar.com.mooral.pojo.BasePojo;
import ar.com.mooral.pojo.GeneralStatusPojo;
import ar.com.mooral.pojo.MedicationListPojo;
import ar.com.mooral.pojo.MedicationPojo;
import ar.com.mooral.pojo.MissedMedicationPojo;
import ar.com.mooral.pojo.PainPojo;
import ar.com.mooral.pojo.util.PojoComparatorDate;
import ar.com.mooral.pojo.util.PojoComparatorName;


public class MedicacionPerdida extends CrohnActivity {

	
	private MissedMedicationPojo pojo;
	
	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medicacion_perdida);

        // RETORNAR
        Button lblMedicacionPerdidaRetorno = (Button) findViewById(R.id.btnMedicacionPerdidaRetorno);
        lblMedicacionPerdidaRetorno.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	finish();
		    }
		});
                
        // SELECCION FECHA Y HORA
        Button btnMedicacionPerdidaFecha = (Button) findViewById(R.id.btnMedicacionPerdidaFecha);
        btnMedicacionPerdidaFecha.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
				Intent intent = new Intent();
				intent.setComponent(new ComponentName(SeleccionFechaHora.class.getPackage().getName(), SeleccionFechaHora.class.getName()));
				startActivity(intent);
		    }
		});
    }
    
	protected void onStart() {
		super.onStart();

		
		if(!CrohnContext.isActivityMissedMedicationLoaded()){
			CrohnContext.setActivityMissedMedicationLoaded(true);
		}else{
			CrohnContext.setActivityMissedMedicationLoaded(false);
			//finish();
		}
		
        if (!CrohnContext.isPojoLoaded()) {
        	MissedMedicationPojo newPojo = new MissedMedicationPojo();
			newPojo.setDate(CrohnContext.getDateTimeNow("dd/MM/yyyy"));
			CrohnContext.setPojo(newPojo);
		}
        
		pojo = (MissedMedicationPojo) CrohnContext.getPojo();
        ((Button) findViewById(R.id.btnMedicacionPerdidaFecha)).setText(CrohnContext.getHumanDate(pojo.getDate()));

		
		
        //MedicationsList medicationsList = new MedicationsList(this);
        Medications medications = new Medications(this);
        
		// find all no soft deleted records 
		Hashtable<String, String> data = new Hashtable<String, String>(); 
		data.put("deleteControlField", "0");
		data.put("tomado", "1");
		
        drawGrid(medications.findAll(data));
	}
    
    
    private void drawGrid(Vector data) {
    	
    	// Clean before addings
        LinearLayout records = (LinearLayout)findViewById(R.id.linearLayoutRegistros);
        records.removeAllViews();
        
        
        Collections.sort((Vector<BasePojo>)data, new PojoComparatorName());
        
        
        for (int i = 0; i < data.size(); i++) {

        	MedicationPojo record = (MedicationPojo) data.get(i);
        	
			
	        CrohnButton recordImageView = new CrohnButton(this);
	        if (pojo.getMedication() != null && pojo.getMedication().equals(record.getName())) {
	        	recordImageView.setBackgroundResource(R.drawable.medicacion_perdida_fondo_medicacion_checked);
	        } else {
	        	recordImageView.setBackgroundResource(R.drawable.medicacion_perdida_fondo_medicacion);
	        }
	        
	        android.widget.RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(280, 40);
	        params.addRule(RelativeLayout.CENTER_IN_PARENT);
	        recordImageView.setLayoutParams(params);
			recordImageView.SetText(record.getName());
			recordImageView.setGravity(Gravity.CENTER_HORIZONTAL);
			recordImageView.setPojo(record);
			
			
			recordImageView.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
    		    	
    		    	MedicationPojo mlp = (MedicationPojo) ((CrohnButton) view).getPojo();
    		    	pojo.setMedication(mlp.getName());
    		    			
    				Intent intent = new Intent();
    				intent.setComponent(new ComponentName(MedicacionPerdidaMotivo.class.getPackage().getName(), MedicacionPerdidaMotivo.class.getName()));
    				//startActivity(intent);
    				
             		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
             		parentActivity.startChildActivity("MedicacionPerdidaMotivo", intent);
    				
    		    }
            });
        	RelativeLayout rl = new RelativeLayout(this);
        	rl.addView(recordImageView);
			records.addView(rl);


		    	
        } // for
    	
    }
    
}
